using System;
using MONA.Modules.Marketing.BLL;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditCustomerType : AddEditItemControl
    {
        public event EventHandler SaveClick;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    CustomerType customerType = CustomerType.Load(this.ItemID);
                    if (customerType != null)
                    {
                        txtCustomerType.Text = customerType.Name;
                        txtCustomerPriority.Text = customerType.Priority;
                        txtDescription.Text = customerType.Description;
                    }
                }
            }
        }

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                CustomerType customerType = new CustomerType();
                customerType.Name = txtCustomerType.Text.Trim();
                customerType.Priority = txtCustomerPriority.Text.Trim();
                customerType.Description = txtDescription.Text.Trim();
                if (this.ItemID == 0)
                {
                    returnId = customerType.Insert();
                }
                else
                {
                    customerType.ID = this.ItemID;
                    returnId = customerType.Update();
                }
            }
            return returnId > 0;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
                SaveClick(sender, e);
            this.ReturnToReferrerPage(); this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                CustomerType customerType = new CustomerType();
                customerType.ID = this.ItemID;
                customerType.Delete();
                this.ReturnToReferrerPage();
            }
        }
    }
}